package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Material;
import com.ruoyi.system.domain.Supplier;
import com.ruoyi.system.domain.vo.PurchaseOrderVo;
import com.ruoyi.system.service.IMaterialService;
import com.ruoyi.system.service.ISupplierService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PurchaseOrder;
import com.ruoyi.system.service.IPurchaseOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 采购Controller
 * 
 * @author 华馨悦
 * @date 2025-01-24
 */
@Controller
@RequestMapping("/system/order")
public class PurchaseOrderController extends BaseController
{
    private String prefix = "system/order";

    @Autowired
    private IPurchaseOrderService purchaseOrderService;

    @Resource
    private ISupplierService supplierService;
    @Resource
    private IMaterialService materialService;

    @RequiresPermissions("system:order:view")
    @GetMapping()
    public String order(ModelMap mmap)
    {
        mmap.put("supplierList", supplierService.selectSupplierList(new Supplier()));
        mmap.put("materialList", materialService.selectMaterialList(new Material()));
        return prefix + "/order";
    }

    /**
     * 查询采购列表
     */
    @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PurchaseOrder purchaseOrder)
    {
        startPage();
        List<PurchaseOrder> list = purchaseOrderService.selectPurchaseOrderList(purchaseOrder);
        List<PurchaseOrderVo> purchaseOrderVos = new ArrayList<>();
        list.forEach(po -> {
            Long supplierId = po.getSupplierId();
            Long materialId = po.getMaterialId();
            Supplier supplier = supplierService.selectSupplierById(supplierId);
            Material material = materialService.selectMaterialById(materialId);
            PurchaseOrderVo purchaseOrderVo = new PurchaseOrderVo();
            BeanUtils.copyProperties(po, purchaseOrderVo);
            purchaseOrderVo.setSupplierName(Optional.ofNullable(supplierService.selectSupplierById(supplierId)).map(Supplier::getSupplierName).orElse("供应商信息已被销毁"));
            purchaseOrderVo.setMaterialName(Optional.ofNullable(materialService.selectMaterialById(materialId)).map(Material::getName).orElse("采购项目信息已被销毁"));
            purchaseOrderVos.add(purchaseOrderVo);
        });
        return getDataTable(purchaseOrderVos);
    }

    /**
     * 导出采购列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "采购", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PurchaseOrder purchaseOrder)
    {
        List<PurchaseOrder> list = purchaseOrderService.selectPurchaseOrderList(purchaseOrder);
        ExcelUtil<PurchaseOrder> util = new ExcelUtil<PurchaseOrder>(PurchaseOrder.class);
        return util.exportExcel(list, "采购数据");
    }

    /**
     * 新增采购
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("supplierList", supplierService.selectSupplierList(new Supplier()));
        mmap.put("materialList", materialService.selectMaterialList(new Material()));
        return prefix + "/add";
    }

    /**
     * 新增保存采购
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "采购", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PurchaseOrder purchaseOrder)
    {
        purchaseOrder.setCreatedTime(DateUtils.getNowDate());
        purchaseOrder.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(purchaseOrderService.insertPurchaseOrder(purchaseOrder));
    }

    /**
     * 修改采购
     */
    @RequiresPermissions("system:order:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PurchaseOrder purchaseOrder = purchaseOrderService.selectPurchaseOrderById(id);
        mmap.put("purchaseOrder", purchaseOrder);
        mmap.put("supplierList", supplierService.selectSupplierList(new Supplier()));
        mmap.put("materialList", materialService.selectMaterialList(new Material()));
        return prefix + "/edit";
    }

    /**
     * 修改保存采购
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "采购", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PurchaseOrder purchaseOrder)
    {
        purchaseOrder.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(purchaseOrderService.updatePurchaseOrder(purchaseOrder));
    }

    /**
     * 删除采购
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "采购", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(purchaseOrderService.softDeletePurchaseOrderByIds(ids));
    }
}
